package Thread;

public class ThreadDemo02 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("我是一个线程，正在工作中~~");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //e.printStackTrace();
                    //加上 break后，抛出异常后，线程就会结束
                    break;
                }
            }
            System.out.println("我被中断了");
        });

        t.start();
        Thread.sleep(3000);
        //使用interrupt()方法中断线程
        System.out.println("我准备中断线程");
        t.interrupt();
    }
}
